package com.skivingcloud.admin.sys.service.impl;

import com.skivingcloud.admin.sys.entity.SysDictionary;
import com.skivingcloud.admin.sys.mapper.SysDictionaryMapper;
import com.skivingcloud.admin.sys.service.ISysDictionaryService;
import com.skivingcloud.common.contants.Constant;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.TreeUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements ISysDictionaryService {

	/**
	 * 分页查询
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<SysDictionary> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		SysDictionary dictionary = (SysDictionary) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),
				SysDictionary.class);
		QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<SysDictionary>();
		if (StringUtils.isNotBlank(dictionary.getClassCode())) {
			queryWrapper.like("class_code", "%" + dictionary.getClassCode() + "%");
		}
		if (StringUtils.isNotBlank(dictionary.getClassName())) {
			queryWrapper.like("class_name", "%" + dictionary.getClassName() + "%");
		}
		queryWrapper.orderByAsc("class_code", "data_order");
		this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	/**
	 * 根据字典编号取字典数据
	 * 优先获取缓存
	 * @param classCode
	 * @return
	 */
	@Override
	@Cacheable(value = "dics", key = "#classCode + '_queryByClassCode'")
	public List<SysDictionary> queryByClassCode(String classCode) {
		QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<SysDictionary>();
		queryWrapper.eq("class_code", classCode);
		queryWrapper.orderByAsc("data_order");
		return this.baseMapper.selectList(queryWrapper);
	}

	@Override
	@Cacheable(value = "dics", key = "#id + '_selectById'")
	public SysDictionary selectById(String id) {
		SysDictionary dictionary = this.baseMapper.selectById(id);
		if (StringUtils.isNotBlank(dictionary.getParentId())) {
			SysDictionary parent = this.baseMapper.selectById(dictionary.getParentId());
			if (parent != null) {
				dictionary.setParentName(parent.getClassName() + "/" + parent.getDataName());
			}
		}
		return dictionary;
	}

	@Override
	@Caching(evict = { @CacheEvict(value = "dics", key = "'dicAll'"),
			@CacheEvict(value = "dics", key = "#dictionary.id + '_selectById'"),
			@CacheEvict(value = "dics", key = "#dictionary.classCode + '_queryByClassCode'"),
			@CacheEvict(value = "dics", key = "#dictionary.classCode + '_queryByClassCodeStatusNormal'") })
	public void update(SysDictionary dictionary) {
		this.baseMapper.updateById(dictionary);
	}

	@Override
	@CacheEvict(value = "dics", allEntries = true)
	public void deleteBatch(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public List<TreeUtils> getDicTreeByClassCode(String classCode) throws Exception {
		String[] codes = classCode.split(",");
		List<SysDictionary> list = new ArrayList<SysDictionary>();
		for (String code : codes) {
			list.addAll(queryByClassCode(code));
		}
		List<TreeUtils> treeList = TreeUtils.getTreeList(list, "id", "parentId", "dataName", null, "dataCode");
		return treeList;
	}

	@Override
	@Caching(evict = { @CacheEvict(value = "dics", key = "'dicAll'"),
			@CacheEvict(value = "dics", key = "#dictionary.id + '_selectById'"),
			@CacheEvict(value = "dics", key = "#dictionary.classCode + '_queryByClassCode'"),
			@CacheEvict(value = "dics", key = "#dictionary.classCode + '_queryByClassCodeStatusNormal'") })
	public void saveNew(SysDictionary dictionary) {
		this.baseMapper.insert(dictionary);
	}

	@Override
	public List<Map<String, Object>> getDicMapByclassCode(String classCode) {
		QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("class_code", classCode);
		queryWrapper.eq("state", Constant.COMMON_STATUS_NORMAL);
		queryWrapper.orderByAsc("data_order");
		List<SysDictionary> list = this.baseMapper.selectList(queryWrapper);
		return list.stream().map(dic -> {
			Map<String, Object> m = new HashMap<>();
			m.put("label", dic.getDataName());
			m.put("value", dic.getDataCode());
			return m;
		}).collect(Collectors.toList());
	}
	
}
